package com.sbs.gaoxinqutoilet.service;

import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.sbs.gaoxinqutoilet.http.HttpApis;
import com.sbs.gaoxinqutoilet.http.Request;
import com.sbs.gaoxinqutoilet.interfaces.Constant;
import com.sbs.gaoxinqutoilet.myapp.MyApplication;
import com.sbs.gaoxinqutoilet.utils.HttpUtils;
import com.sbs.gaoxinqutoilet.utils.LogUtils;
import com.sbs.gaoxinqutoilet.utils.appinstallerutils.AppUtils;

import java.io.File;

/**
 * If you change her, tell me
 * sometimes I believe compiler ignores all my comments
 * Created by gyyak46 on 2017/7/21.
 */

public class DownLoadService extends Service {

    private String versionCode;


    @Override
    public void onCreate() {
        super.onCreate();

    }

    //@IntDef(value = {Service.START_FLAG_REDELIVERY, Service.START_FLAG_RETRY}, flag = true)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            versionCode = intent.getStringExtra(Constant.VERSION_CODE);
        }
        //判断有没有当前版本安装包
        deleteLocalCode();
        //获取路径有没有新版本的安装包
        String strPath = Environment
                .getExternalStorageDirectory()
                .getAbsolutePath() + "/" + "yzd" + versionCode + "newCode.apk";

        final File strFile = new File(strPath);

        LogUtils.e("strPath", strPath + true);
        //  如果文件存在
        if (strFile.exists()) {
            LogUtils.e("strFile", " 存在" + strFile.getAbsolutePath());
            AppUtils.installApp(MyApplication.sContext,strFile.getAbsolutePath());
            stopSelf();
        } else {
            //下载
            downloadNewApp(versionCode);
        }
        return START_NOT_STICKY;
    }

    private void deleteLocalCode() {

        int appVersionCode = AppUtils.getAppVersionCode(MyApplication.sContext);
        String strPath = Environment
                .getExternalStorageDirectory()
                .getAbsolutePath() + "/" + "yzd" + versionCode + "newCode.apk";
        File strFile = new File(strPath);
        LogUtils.e("strPath", strPath + true);
        //  如果文件存在
        if (strFile.exists()) {
            strFile.delete();
            LogUtils.e("strFile", "删除成功");
        }
    }

    private void downloadNewApp(String versionCode) {
        HttpUtils.requestDownloadFile(getApplicationContext(), HttpApis.DOWN_UPDATE, "", "yzd" + versionCode + "newCode.apk", new Request() {
            @Override
            public void successFile(File file) {
                super.successFile(file);

                LogUtils.e("file", file.length() / 1024 + " " + file.getAbsolutePath());
                AppUtils.installApp(MyApplication.sContext, file.getAbsolutePath());
                stopSelf();

            }

            @Override
            public void error(String error) {
                super.error(error);
            }
        });


    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


}
